package com.ag.lzw.action;

import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.ag.bean.TbTask;
import com.ag.bean.TbTaskFeedback;
import com.ag.bean.TbUser;
import com.ag.common.BaseAction;
import com.ag.common.EventUtil;
import com.ag.common.IPermissionService;
import com.ag.common.PageUtil;
import com.ag.lzw.dao.LzwTaskDAO;
import com.ag.lzw.dao.LzwTbFeedbackAnnexDAO;
import com.ag.lzw.dao.LzwTbTaskFeedbackDAO;
import com.ag.lzw.dao.LzwTbUserDAO;
import com.ag.lzw.service.HandelTasks;
import com.ag.lzw.service.IdentifyService;
import com.ag.lzw.service.TaskTrackHandel;
import com.ag.lzw.vo.TaskDetail;

@Scope("prototype")
@Controller("taskTrackAction")
public class TaskTrackAction extends BaseAction implements SessionAware {

	private static final long serialVersionUID = -3611835484373755066L;

	private static final Logger logger = Logger
			.getLogger(TaskTrackAction.class);

	private String eid;
	private String leader;

	// private static final String userID="ag";

	@Resource(name = "handelTaskImpl")
	private HandelTasks taskHandeler;
	@Resource(name = "TaskTrackHandelImpl")
	private TaskTrackHandel taskTrackHandel;
	@Resource(name = "lzwTbTaskFeedbackDAO")
	private LzwTbTaskFeedbackDAO taskFeedBackDAO;
	@Resource(name = "lzwTbFeedbackAnnexDAO")
	private LzwTbFeedbackAnnexDAO feedbackAnnexDAO;
	@Resource(name = "lzwTbUserDAO")
	private LzwTbUserDAO userDAO;
	@Resource(name = "IdentifyServiceImpl")
	private IdentifyService identifyServ;
	@Resource(name = "lzwTaskDao")
	private LzwTaskDAO taskDAO;

	/**
	 * 调用权限服务逻辑组件
	 */
	@Resource(name = "PermissionServiceImpl")
	private IPermissionService pms;

	/**
	 * 提供Map<String, Object>类型的Session变量，用来保存Struts2注入的Session上下文
	 */
	private Map<String, Object> sessionMap;

	private Map<String, Object> myUserSessionMap;

	/**
	 * 用户信息返回载体
	 */
	private TbUser tbUser;

	@Override
	public void setSession(Map<String, Object> session) {
		this.sessionMap = session;
	}

	/**
	 * 根据当前用户的登录信息，查询属于其任务
	 * 
	 */
	public String queryMyTasks() {
		TbUser currentUser = this.pms.getCurrentUser(sessionMap);
		String uid = currentUser.getUId(); // 获取当前用户ID
		logger.info("Current userID:" + uid);
		boolean isLeader = false;

		try {
			if (getSessionAttribute(EventUtil.PROCESSING_EVENT_ID) != null) {
				// 调用service分辨当前用户是否指挥长
				isLeader = identifyServ.isLeader(uid,
						getSessionAttribute(EventUtil.PROCESSING_EVENT_ID));
			}
		} catch (Exception e) {
			logger.debug(e);
		}

		logger.info("It is a leader?:" + isLeader);
		PageUtil pu = this.taskHandeler.queryTaskByPage(queryOption, uid,
				isLeader);

		if (pu != null) {
			userTasks = pu;
			return "queryUserTask_Succ";
		} else {
			errMsg = "当前没有属于你的任务！";
			return "queryUserTask_Fail";
		}
	}

	public String isLeader() {
		TbUser currentUser = this.pms.getCurrentUser(sessionMap);
		String uid = currentUser.getUId(); // 获取当前用户ID
		logger.info("Current userID:" + uid);
		boolean isLeader = false;
		System.out.println("++++++++++++++++eid++=========="+eid);
		try {
			// 调用service分辨当前用户是否指挥长
			isLeader = identifyServ.isLeader(uid, eid);
		} catch (Exception e) {
			logger.debug(e);
		}
		if (isLeader == true) {
			this.setLeader("1");
		} else {
			this.setLeader("2");
		}
		return "isLeader";
	}

	/**
	 * 根据任务ID查询任务的详情
	 * 
	 * @return
	 */
	public String queryTaskDetail() {
		TaskDetail td = taskHandeler.queryTaskById(taskid);
		if (td != null) {
			logger.info("Return task id:" + td.getId() + td.getName());
			taskDetail = td;
			
			TbUser currentUser = this.pms.getCurrentUser(sessionMap);
			String uid = currentUser.getUId(); // 获取当前用户ID
			logger.info("Current userID:" + uid);
			TbTask tt = taskDAO.findTaskDetailById(taskid);
			logger.info("Current Task of uid:"+tt.getTbUser().getUId());
			if(tt.getTbUser().getUId().equals(uid)){
				errMsg = "000"; // 当前用户为任务负责人，返回001作为标识
			} else {
				errMsg = "001"; 
			}
			return "queryTaskDetail_Succ";
		} else {
			errMsg = "查询失败，请重试！";
			return "queryTaskDetail_Fail";
		}
	}

	/**
	 * 用户接受任务
	 * 
	 * @return
	 */
	public String acceptTask() {
		logger.info("Accept Task id:" + taskid);
		if (!(taskid.equals("null")) && taskid.length() > 0) {
			taskHandeler.changeTaskStatus(taskid, "1001");
			this.errMsg = "1";
			return "accepttask";
		} else {
			this.errMsg = "0";
			return "accepttask_false";
		}
	}

	public String refuseTask() {
		logger.info("Refuse Task id:" + taskid);
		if (!(taskid.equals("null")) && taskid.length() > 0) {
			taskHandeler.changeTaskStatus(taskid, "1002");
			this.errMsg = "1";
			return "refusetask";
		} else {
			this.errMsg = "0";
			return "refusetask_false";
		}
	}

	/**
	 * 分页查询任务反馈
	 * 
	 * @return
	 */
	public String queryTrackByPage() {
		logger.info("finding tracklist");

		logger.info("queryOption size:" + queryOption.size());

		PageUtil pu = taskTrackHandel.getTrackListByPage(queryOption);
		if (pu != null) {
			logger.info("queryTrackByPage Succ!");
			trackList = pu;
			return "queryTrackByPage_Succ";
		} else {
			errMsg = "分页查询任务反馈失败！";
			return "queryTrackByPage_Fail";
		}

	}

	/**
	 * 查询任务反馈的详情
	 * 
	 * @return
	 */
	public String queryTrackDetail() {
		logger.info("queryTrackDetail by id:" + feebackid);
		// 获取当前用户的信息
		TbUser currentUser = this.pms.getCurrentUser(sessionMap);
		String uid = currentUser.getUId(); // 获取当前用户ID
		logger.info("Current userID:" + uid);
		TbUser tu = userDAO.findById(uid);

		TbTaskFeedback ttf = new TbTaskFeedback();
		ttf = taskFeedBackDAO.findByFBId(feebackid);

		// 当反馈的状态为代码“0”时
		if (ttf.getTfCheck().equals("0")) {
			// 查看反馈者非反馈的作者时，改变该反馈的状态
			boolean is_author = ttf.getTfPeople().equals(tu.getUName());
			if (!is_author) {
				ttf.setTfCheck("1");
				taskFeedBackDAO.attachDirty(ttf);
			}
		}

		if (ttf != null) {
			logger.info("get a feedback id:" + ttf.getTfId());
			this.getRequest().setAttribute("feedback", ttf);
			this.getRequest().setAttribute("annex", ttf.getTbFeedbackAnnexes());
			if (ttf.getB().length() > 0) {
				this.getRequest().setAttribute("tfNote", ttf.getB());
			} else {
				this.getRequest().setAttribute("tfNote", null);
			}

			return "queryTrackDetail_Succ";
		} else {
			return "queryTrackDetail_Fail";
		}
	}

	private TbTaskFeedback feedback;

	public TbTaskFeedback getFeedback() {
		return feedback;
	}

	public void setFeedback(TbTaskFeedback feedback) {
		this.feedback = feedback;
	}

	/**
	 * 传回前台的结果集合
	 */
	private PageUtil detailtasks;

	/**
	 * 分页查询的参数
	 */
	private Map<String, String> queryOption;
	/**
	 * 任务ID
	 */
	private String taskid;
	/**
	 * 反馈信息列表
	 */
	private PageUtil trackList;

	private PageUtil userTasks;

	private TaskDetail taskDetail;

	private String errMsg;

	private String querypage;

	private String feebackid;

	// 以下是属性的get，set方法

	public String getFeebackid() {
		return feebackid;
	}

	public void setFeebackid(String feebackid) {
		this.feebackid = feebackid;
	}

	public TaskDetail getTaskDetail() {
		return taskDetail;
	}

	public PageUtil getUserTasks() {
		return userTasks;
	}

	public void setUserTasks(PageUtil userTasks) {
		this.userTasks = userTasks;
	}

	public String getErrMsg() {
		return errMsg;
	}

	public PageUtil getDetailtasks() {
		return detailtasks;
	}

	public void setQueryOption(Map<String, String> queryOption) {
		this.queryOption = queryOption;
	}

	public Map<String, String> getQueryOption() {
		return queryOption;
	}

	public PageUtil getTrackList() {
		return trackList;
	}

	public void setTaskid(String taskid) {
		this.taskid = taskid;
	}

	public void setTrackList(PageUtil trackList) {
		this.trackList = trackList;
	}

	public String getQuerypage() {
		return querypage;
	}

	public void setQuerypage(String querypage) {
		this.querypage = querypage;
	}

	public String getEid() {
		return eid;
	}

	public void setEid(String eid) {
		this.eid = eid;
	}

	public String getLeader() {
		return leader;
	}

	public void setLeader(String leader) {
		this.leader = leader;
	}

}
